﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.IO;
using System.Net.Mime;

namespace IECAS.ERS.Services
{
    public class MailSender
    {
        private SmtpClient _smtpClient = null;

        public SmtpClient SmtpClient
        {
            get { return _smtpClient; }
            set { _smtpClient = value; }
        }

        /// <summary>
        /// from:pyxuzhisong@126.com
        /// smtpHost:smtp.126.com;
        /// username:pyxuzhisong;
        /// password:3901053;
        /// </summary>
        /// <param name="tos">xuzhisong520@126.com</param>
        /// <param name="ccTos"></param>
        /// <param name="bccTos"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="BodyFormat"></param>
        /// <param name="Priority"></param>
        /// <param name="attachments"></param>
        /// <param name="priority"></param>
        /// <param name="cookie"></param>
        public void SendEmailAsync(
            string from,
            string[] tos,
            string[] ccTos,
            string[] bccTos,
            string subject, string body, int BodyFormat, 
            Attachment[] attachments,
            MailPriority priority,
            string messageId,
            string smtpHost, int smtpPort, bool enableSSL,
            string username,
            string password,
            object cookie
            )
        {
            SmtpClient smtpClient = new SmtpClient(smtpHost, smtpPort);
            smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
            if (username.Length > 0 || password.Length > 0)
            {
                smtpClient.Credentials = new System.Net.NetworkCredential(username, password);
            }
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl = enableSSL;
            //邮件信息
            MailMessage mailMessage = new MailMessage();
            mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.Delay | DeliveryNotificationOptions.OnFailure;
            mailMessage.From = new MailAddress(from);
            if (tos != null && tos.Length != 0)
            {
                foreach (string to in tos)
                    mailMessage.To.Add(to);
            }
            if (ccTos != null && ccTos.Length != 0)
            {
                foreach (string cc in ccTos)
                    mailMessage.To.Add(cc);
            }
            if (bccTos != null && bccTos.Length != 0)
            {
                foreach (string bcc in bccTos)
                    mailMessage.To.Add(bcc);
            }
            //主题
            mailMessage.Subject = subject;
            //内容
            mailMessage.Body = body;
            //正文编码
            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
            //设置为HTML格式
            mailMessage.IsBodyHtml = true;
            mailMessage.Headers.Add("Disposition-Notification-To", from); //一般邮件实现邮件回执
            mailMessage.Headers.Add("ReturnReceipt", "1");                //notes服务器,实现“已读回执”。
            if (!string.IsNullOrWhiteSpace(messageId))
                mailMessage.Headers.Add("Message-ID", "<" + messageId.Replace("<", "").Replace(">", "") + ">");
            //mailMessage.Headers.Add("In-Reply-To", "<578d518c.ff53.12b1672f6fe.Coremail.xuzhisong520@126.com>");
            //mailMessage.Headers.Add("References", "<578d518c.ff53.12b1672f6fe.Coremail.xuzhisong520@126.com>");
            //优先级
            mailMessage.Priority = priority;
            if (attachments != null && attachments.Length != 0)
            {
                foreach (Attachment attachment in attachments)
                {
                    mailMessage.Attachments.Add(attachment);
                }
            }
            EmailCookieData cookieData = new EmailCookieData();
            cookieData.Cookie = cookie;
            cookieData.Message = mailMessage;
            smtpClient.SendAsync(mailMessage, cookieData);
        }

        public event EventHandler<EmailSendCompleteEventArgs> SendCompleted;

        private void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            EmailCookieData cookieData = e.UserState as EmailCookieData;

            if (cookieData != null && SendCompleted != null)
            {
                SendCompleted(this, new EmailSendCompleteEventArgs(e, cookieData.Message, cookieData.Cookie));
            }
        }
    }

    public class EmailSendCompleteEventArgs : System.ComponentModel.AsyncCompletedEventArgs
    {
        public MailMessage Message;

        public EmailSendCompleteEventArgs(System.ComponentModel.AsyncCompletedEventArgs e, MailMessage message, object cookie)
            : base(e.Error, e.Cancelled, cookie)
        {
            this.Message = message;
        }
    }

    public class EmailCookieData
    {
        public MailMessage Message;
        public object Cookie;
    }
}